import Cookies from 'js-cookie'

const user = {
  state: {
    userName: '',
    roleName: '',
    telephone: '',
    roleId: '',
    branches: [],
    client: {},
    isMigrateClient: true,
    clientId: '',
    isMogoroomAuth: false,
    devicePermission: true, // 是否有设备添加和解绑权限
    hasFaceAccessPermission: false, // 是否有人脸门禁管理的权限
    isVerifyPrivacy:
      (window &&
        window.localStorage &&
        window.localStorage.getItem('is_verify_privacy')) ||
      false
  },
  mutations: {
    logout (state) {
      Cookies.remove('user')
      Cookies.remove('password')
      Cookies.remove('access')
      // 恢复默认样式
      // let themeLink = document.querySelector('link[name="theme"]');
      // themeLink.setAttribute('href', '');
      // 清空打开的页面等数据，但是保存主题数据
      let theme = ''
      if (localStorage.theme) {
        theme = localStorage.theme
      }
      localStorage.removeItem('is_verify_privacy')
      localStorage.removeItem('permission')
      localStorage.removeItem('last_login_req_id')
      if (theme) {
        localStorage.theme = theme
      }
    },
    updateUserInfo (state, res) {
      const keys = [
        'bind_lock',
        'unbind_lock',
        'bind_gateway',
        'unbind_gateway',
        'bind_elecollector',
        'unbind_elecollector',
        'bind_elemeter',
        'unbind_elemeter',
        'bind_water_gateway',
        'unbind_water_gateway',
        'bind_watermeter',
        'unbind_watermeter'
      ]
      if (res && res.client_optional_permissions) {
        // const flag = true;
        const permissions_keys = []
        let hasFaceAccessPermission = false

        res.client_optional_permissions.forEach(permission => {
          const { resource, action } = permission
          if (resource === 'room') {
            permissions_keys.push(action)
          }

          if (resource === 'client' && action === 'face_access') {
            hasFaceAccessPermission = true
          }
        })

        state.devicePermission =
          permissions_keys.length ===
          [...new Set(permissions_keys.concat(keys))].length
        state.hasFaceAccessPermission = hasFaceAccessPermission
      } else {
        state.devicePermission = false
      }
      state.userName = res.name
      state.telephone = res.telephone
      state.roleName = res.role.name
      state.roleId = Number(res.role.id)
      state.branches = res.branches
      state.client = res.client
      state.clientId = res.client_id
      state.isMogoroomAuth = res.client.is_mogoroom_auth
      if (
        res.is_verify_privacy &&
        window &&
        window.localStorage &&
        window.localStorage.setItem
      ) {
        window.localStorage.setItem(
          'is_verify_privacy',
          res.is_verify_privacy
        )
      }
      state.isVerifyPrivacy = res.is_verify_privacy
      const tags = res.client.tags
      if (tags && Array.isArray(tags) && tags.includes('1000')) {
        state.isMigrateClient = true
      } else {
        state.isMigrateClient = false
      }
    }
  },
  getters: {
    isMogoroomAuth: state => state.isMogoroomAuth,
    hasFaceAccessPermission: state => state.hasFaceAccessPermission,
    isVerifyPrivacy: state => state.isVerifyPrivacy
  }
}

export default user
